<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * CakePHP InstagramHelper
 * @author lucas
 */
App::uses('HttpSocket', 'Network/Http');

class InstagramHelper extends AppHelper {

    public $helpers = array();
    public $_http = NULL;
    public $_max_pagination = 0;
    public $_numero_pagination = 0;
    public $_access_token = NULL;

    public function __construct(View $View, $settings = array()) {
        parent::__construct($View, $settings);
        $this->_http = new HttpSocket();
        $this->__getToken();
    }

    public function getUser($usuario) {
        //https://api.instagram.com/v1/users/search?q=myride&access_token=202610441.588ca4a.05373fc5ca884e6f938a50f52054250d
        $response = $this->_http->get('https://api.instagram.com/v1/users/search?q=' . trim($usuario) . '&access_token=' . $this->_access_token);

        if ($response->code == 200) {
            return json_decode($response->body, true);
        } else {
            return null;
        }
    }

    public function getMedia($id_user) {
        //https://api.instagram.com/v1/users/8319259/media/recent/?access_token=202610441.588ca4a.05373fc5ca884e6f938a50f52054250d
        $response = $this->_http->get('https://api.instagram.com/v1/users/' . trim($id_user) . '/media/recent/?access_token=' . $this->_access_token);

        if ($response->code == 200) {
            return json_decode($response->body, true);
        } else {
            return null;
        }
    }

    public function getMediaFile($id_user, $page = 0) {
        //https://api.instagram.com/v1/users/8319259/media/recent/?access_token=202610441.588ca4a.05373fc5ca884e6f938a50f52054250d
        $response = $this->_http->get('https://api.instagram.com/v1/users/' . trim($id_user) . '/media/recent/?access_token=' . $this->_access_token);

        if ($response->code == 200) {
            $this->_max_pagination = $page;
            $result = array();
            $dados = json_decode($response->body, true);
            if (isset($dados['pagination']['next_url'])) {
                $dados = $this->__pagination($dados['pagination']['next_url'], $dados);
            }
            if (isset($dados['data']) AND count($dados['data']) > 0) {
                foreach ($dados['data'] as $key => $value) {
                    $result[$key]['id'] = $value['id'];
                    $result[$key]['type'] = $value['type'];
                    $result[$key]['link'] = $value['link'];
                    $result[$key]['images'] = $value['images'];
                    $result[$key]['caption'] = $value['caption']['text'];
                }
            }

            return $result;
        } else {
            return null;
        }
    }

    private function __pagination($link, $dados = array()) {
        if ($this->_numero_pagination < $this->_max_pagination) {
            $response = $this->_http->get($link);

            if ($response->code == 200) {
                $_dados = json_decode($response->body, true);
                $dados['data'] = array_merge($dados['data'], $_dados['data']);
                if (isset($_dados['pagination']['next_url'])) {
                    $this->_numero_pagination++;
                    $dados = $this->__pagination($_dados['pagination']['next_url'], $dados);
                }
            }
        }
        return $dados;
    }

    private function __getToken() {
        $Menu = ClassRegistry::init('Social.Social');

        $retorno = $Menu->findByTipo('instagram');
        if (count($retorno) > 0) {
            if ($retorno['Social']['token'] != '') {
                $this->_access_token = $retorno['Social']['token'];
                return TRUE;
            }
        }
        return FALSE;
    }

}
